package com.zh.freechat.infrastructure.mq.observer;

import com.zh.freechat.domain.notice.NoticeMessage;
import com.zh.freechat.domain.notice.SseEmitterServer;
import com.zh.freechat.infrastructure.mq.SseMsgMQProducer;
import com.zh.webcommon.common.support.observer.DataObserver;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * @author Zhou
 * @date 18:10 2024/3/30
 */
@Slf4j
@AllArgsConstructor
@Component
public class SseMsgMQConsumer implements DataObserver<NoticeMessage>, InitializingBean {
    private final SseEmitterServer sseEmitterServer;
    private SseMsgMQProducer sseMsgMQProducer;

    @Override
    public void receive(NoticeMessage message) {
        sseEmitterServer.sendMessage(message);
    }

    @Override
    public void register() {
        sseMsgMQProducer.register(this);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        register();
    }
}
